package com.work.demo.juctest;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName : Test
 * @Description :线程安全三：如何保证原子性
 * @Author : LinYa
 * @Date: 2021/11/15  14:18
 */
public class Test {

//	private static int sum = 0;

	private static AtomicInteger sum = new AtomicInteger(0);

	static Object object = "";
	static ReentrantLock lock = new ReentrantLock();


	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			new Thread(() -> {
//				lock.lock();
//				try {
//					for (int j = 0; j < 10000; j++) {
//						sum++;
//					}
//				}finally {
//					lock.unlock();
//				}

				for (int j = 0; j < 10000; j++) {
					sum.incrementAndGet();
				}
			}).start();
		}

		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		System.out.println(sum);
	}

}
